package com.hzhjhh.view.cashierView;

import java.awt.Font;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.util.Objects;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import org.apache.commons.lang3.StringUtils;

import com.hzhjhh.dto.MerchandiseDTO;
import com.hzhjhh.entity.Merchandise;
import com.hzhjhh.mapper.MerchandiseMapper;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.util.FieldMapping;
import com.hzhjhh.view.cashierView.constant.ShowConstants;
import com.hzhjhh.view.cashierView.dto.MerchandiseShowVO;
import com.hzhjhh.view.cashierView.enums.MerchandiseShowEnum;

import lombok.extern.slf4j.Slf4j;

/**
 * 组件初始化-收银页面-商品数量
 * @author 12083
 *
 */
@Slf4j
public class MerchandiseNumInit {

	private static MerchandiseMapper merchandiseMapper = DBUtils.getSqlSession(true).getMapper(MerchandiseMapper.class);
	
	/**
	 * 初始化商品数量-标签
	 * @return
	 */
	public static JLabel initLabel() {
		JLabel merchandiseNum = new JLabel();
		merchandiseNum.setBounds(267, 463, 42, 15);
		merchandiseNum.setText("数量");
		return merchandiseNum;
	}
	
	/**
	 * 初始化商品数量-输入框
	 * 
	 * @param txfPriceOne 商品单价输入框
	 */
	public static JTextField initTxf(JTextField merchandiseCode, JLabel receivable,JTextField txfPricePerOne, JTextField txfDiscount, JScrollPane jScrollPane) {
		JTextField merchandiseNum = new JTextField();
		merchandiseNum.setFont(new Font("宋体", Font.PLAIN, 20)); 
		merchandiseNum.setText("1");
		merchandiseNum.setBounds(302, 460, 72, 21);
		// 添加失焦事件
		merchandiseNum.addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				queryDB(merchandiseCode, merchandiseNum, receivable, txfPricePerOne, txfDiscount, jScrollPane);
			}
		});
		// 添加按键事件
		merchandiseNum.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (KeyEvent.VK_ENTER != e.getKeyCode()) {
					// 不是回车键，不做处理
					return;
				}
				queryDB(merchandiseCode, merchandiseNum, receivable, txfPricePerOne, txfDiscount, jScrollPane);
			}
		});
		return merchandiseNum;
	}
	
	/**
	 * 事件触发后执行的动作
	 * 
	 * @param merchandiseCode 商品编码输入框
	 * @param merchandiseNum 商品数量输入框
	 * @param receivable 应收输入框
	 * @param txfPricePerOne 单价输入框
	 * @param txfDiscount 折扣输入框
	 * @param jScrollPane 商品信息展示滚动板
	 */
	private static void queryDB(JTextField merchandiseCode, JTextField merchandiseNum, 
			JLabel receivable, JTextField txfPricePerOne,
			JTextField txfDiscount, JScrollPane jScrollPane) {
		String codeInput = merchandiseCode.getText();
		String numInput = StringUtils.trim(merchandiseNum.getText());
		if (StringUtils.isAnyBlank(codeInput, numInput)) {
			JOptionPane.showMessageDialog(merchandiseCode, "商品编号，商品数量全部不能为空", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		int quantity ;
		int discount;
		String discountStr = txfDiscount.getText();
		try {
			quantity = Integer.parseInt(numInput);
			discount = Integer.parseInt(discountStr);
		} catch (Exception e) {
			log.error("商品数量/折扣解析失败：{}", numInput);
			JOptionPane.showMessageDialog(merchandiseNum, String.format("商品数量/折扣解析失败，numInput:%s，discountStr:%s", numInput, txfDiscount), "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		Vector<String> headV = ShowConstants.MERCHANDISE_HEAD;

		// 根据商品编码查询商品信息
		MerchandiseDTO merchandiseDTO = new MerchandiseDTO();
		merchandiseDTO.setMerchandiseTreatyCode(codeInput);
		Merchandise merchandise = merchandiseMapper.selectOneByDTO(merchandiseDTO);
		if (Objects.isNull(merchandise)) {
			JOptionPane.showMessageDialog(merchandiseCode, "商品不存在", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		BigDecimal pricePerOneDB = merchandise.getMerchandiseRetailPrice();
		txfPricePerOne.setText(pricePerOneDB.toPlainString());
		MerchandiseShowVO vo = MerchandiseShowVO.getFromEntity(merchandise, quantity, discount);
		Vector<String> showValue = FieldMapping.entity2Show(vo, MerchandiseShowEnum.descMap(), headV);
		Vector<Vector<String>> rowDatas = new Vector<>();
		rowDatas.add(showValue);
		jScrollPane.getViewport().add(
			new JTable(rowDatas, headV)
		); // 把表装入容器
		receivable.setText(vo.getAmt());
	}
	
	public static JTextField initTxf(VenditionDialog venditionDialog) {
		JTextField merchandiseNum = new JTextField();
		merchandiseNum.setFont(new Font("宋体", Font.PLAIN, 20)); 
		merchandiseNum.setText("1");
		merchandiseNum.setBounds(302, 460, 72, 21);
		// 添加失焦事件
		merchandiseNum.addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				queryDB(venditionDialog);
			}
		});
		// 添加按键事件
		merchandiseNum.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (KeyEvent.VK_ENTER != e.getKeyCode()) {
					// 不是回车键，不做处理
					return;
				}
				queryDB(venditionDialog);
			}
		});
		return merchandiseNum;
	}
	private static void queryDB(VenditionDialog venditionDialog) {
		JTextField merchandiseCode = venditionDialog.getTxfMerchandiseCode();
		JTextField merchandiseNum = venditionDialog.getTxfMerchandiseNum();
		JLabel receivable = venditionDialog.getLbReceivable2();
		JTextField txfPricePerOne = venditionDialog.getTxfPricePerOne();
		JTextField txfDiscount = venditionDialog.getTxfDiscount();
		JScrollPane jScrollPane = venditionDialog.getJScrollPane1();
		
		String codeInput = merchandiseCode.getText();
		String numInput = StringUtils.trim(merchandiseNum.getText());
		if (StringUtils.isAnyBlank(codeInput, numInput)) {
			JOptionPane.showMessageDialog(merchandiseCode, "商品编号，商品数量全部不能为空", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		int quantity ;
		int discount;
		String discountStr = txfDiscount.getText();
		try {
			quantity = Integer.parseInt(numInput);
			discount = Integer.parseInt(discountStr);
		} catch (Exception e) {
			log.error("商品数量/折扣解析失败：{}", numInput);
			JOptionPane.showMessageDialog(merchandiseNum, String.format("商品数量/折扣解析失败，numInput:%s，discountStr:%s", numInput, txfDiscount), "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		Vector<String> headV = ShowConstants.MERCHANDISE_HEAD;

		// 根据商品编码查询商品信息
		MerchandiseDTO merchandiseDTO = new MerchandiseDTO();
		merchandiseDTO.setMerchandiseTreatyCode(codeInput);
		Merchandise merchandise = merchandiseMapper.selectOneByDTO(merchandiseDTO);
		if (Objects.isNull(merchandise)) {
			JOptionPane.showMessageDialog(merchandiseCode, "商品不存在", "错误", JOptionPane.ERROR_MESSAGE);
			return;
		}
		BigDecimal pricePerOneDB = merchandise.getMerchandiseRetailPrice();
		txfPricePerOne.setText(pricePerOneDB.toPlainString());
		MerchandiseShowVO vo = MerchandiseShowVO.getFromEntity(merchandise, quantity, discount);
		Vector<String> showValue = FieldMapping.entity2Show(vo, MerchandiseShowEnum.descMap(), headV);
		Vector<Vector<String>> rowDatas = venditionDialog.getRowDatas();
		rowDatas.add(showValue);
		JTable jtable = new JTable(rowDatas, headV);
		venditionDialog.setJTable1(jtable);
		jScrollPane.getViewport().add(
				jtable
		); // 把表装入容器
		receivable.setText(vo.getAmt());
	}
}
